home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4965 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.6 KB  |  99 lines

  1. Newsgroups: comp.lang.c++
  2. Path: usinternet.com!not-for-mail
  3. From: Scott Jibben <sjibben@usinternet.com>
  4. Subject: Re: Pointers to member functions HOW?
  5. Message-ID: <31113B7A.4C82@usinternet.com>
  6. Date: Thu, 01 Feb 1996 16:15:22 -0600
  7. Organization: Jibben Software
  8. X-Mailer: Mozilla 2.0b6a (WinNT; I)
  9. MIME-Version: 1.0
  10. References: <31067074.6B53@compuserve.com> <4e9nh8$iji@news2.ios.com>
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13.  
  14. Vlastimil Adamovsky wrote:
  15. > Roberto Ortiz <74011.3205@compuserve.com> wrote:
  16. > >I've been attempting for some time now to declare a pointer to a class'
  17. > >memeber function. I have no problem with pointers to regular functions, but
  18. > >with class members, I run into one of two problems:
  19. > >a) I can't assign the function to the pointer.
  20. > >b) I can't call the pointer as a function.
  21. > >whenever I solve one, I get the other.
  22. > >Here's some example code in Borland C++:
  23. > >#include <stdio.h>
  24. > >void RegularFunction(int iVal)
  25. > >{
  26. > >       printf("[%d]\n", iVal);
  27. > >};
  28. > >class TTest {
  29. > >public:
  30. > >       void MemberFunction(int iVal)
  31. > >       {
  32. > >               printf("[%d]\n", iVal);
  33. > >       };
  34. > >};
  35. > >void main()
  36. > >{
  37. > >       typedef void (TFuncVoidInt)(int);
  38. > >       TFuncVoidInt    *Func0;
  39. > >       TFuncVoidInt    *Func1;
  40. > >       void (TTest::*Func2)(int);
  41. > >       Func0 = RegularFunction;
  42. > >       Func1 = TTest::MemberFunction;
  43. > >       Func2 = &TTest::MemberFunction;
  44. > >       Func0(100);
  45. > >       Func1(100);
  46. > >       Func2(100);
  47. > >};
  48. > >with this code, I get the following compiler messages:
  49. > >Compiling PTEST.CPP:
  50. > >Error PTEST.CPP 26: Cannot convert 'void (TTest::*)(int)' to 'void (*)(int)'
  51. > >in function main()
  52. > >Error PTEST.CPP 31: Call of nonfunction in function main()
  53. > >function main()
  54. > The compiling error messages are very correct:
  55. >  1) you cannot cast member function to non-member. It is nonsense
  56.  
  57. You can if the method is declared as static:
  58.  
  59. class TTest {
  60. public:
  61.        static void MemberFunction(int iVal)
  62.        {
  63.                printf("[%d]\n", iVal);
  64.        };
  65. };
  66.  
  67. This only allows for one instance of the method for all 
  68. implementations of the class, much like a static member.
  69.  
  70. -- 
  71. Scott Jibben, Jibben Software
  72. Galactic Overlord & Mines of Gorr BBS Door Games
  73. -----------------------------
  74. [EMAIL] sjibben@usinternet.com
  75. [WWW] http://www.usinternet.com/jsw
  76. [FTP] ftp.europa.com /outgoing/DOORS/jibben
  77. [FTP] ftp.cts.com /pub/dferber/jibben
  78. [FIDO] 1:282/115             [BBS] 612-379-8272 (10 USR 28.8 
  79. Couriers)
  80. [VOICE] 612-757-5626         [FAX] 612-757-8687
  81.  
  82.